package boot.controller.upload;

import boot.common.CommonResult;
import boot.domain.travel.Travel;
import boot.domain.wenchuang.Wenchuang;
import boot.domain.wxGoods.Goods;
import boot.service.travelService;
import boot.service.wenchuangService;
import boot.service.wxGoodService;
import boot.service.wxGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import boot.util.uploadImgUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
@CrossOrigin
@RestController
public class UploadImg {
    @Resource
    private uploadImgUtil uploadImgUtil ;
    @Autowired
    private wxGoodService wxGoodService;
    @Autowired
    private wenchuangService wenchuangService;
    @Autowired
    private travelService travelService;
    @Autowired
    private wxGoodsCategoryService wxGoodsCategoryService;
    @PostMapping("/uploadImg")
    public CommonResult uploadImg(@RequestParam("picture") MultipartFile file){
        String imgpath;
        try {
            System.out.println(file);
            imgpath  = uploadImgUtil.uploadFile(file);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult(400,"图片异常！",null);
        }
        return new CommonResult(200,"",imgpath);
    }
    @PostMapping("/wxUploadGoods")
    public CommonResult wxUploadGoods(@RequestParam("picture") MultipartFile file, @RequestParam("name")String name, @RequestParam("price") BigDecimal price, @RequestParam("category") String category, @RequestParam("description") String description, @RequestParam("content") String content,@RequestParam("type") int type) {
        String imgpath;
        try {
            System.out.println(file);
            imgpath  = uploadImgUtil.uploadFile(file);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult(400,"图片异常！",null);
        }
        if(type==1){
            int id = wxGoodsCategoryService.findId(category);
            Goods goods = new Goods(null,id,name,imgpath,description,content,price);
//        System.out.println(goods);
            if(wxGoodService.addGood(goods)){
                return new CommonResult(200,"插入成功！",null);
            }else {
                return new CommonResult(404,"插入失败！",null);
            }
        }else if(type==2){
            Wenchuang wenchuang = new Wenchuang(null,name,imgpath,description,content,price);
//        System.out.println(goods);
            if(wenchuangService.addWenchuang(wenchuang)){
                return new CommonResult(200,"插入成功！",null);
            }else {
                return new CommonResult(404,"插入失败！",null);
            }
        }else {
            Travel travel = new Travel(null,name,imgpath,description,content,price);
//        System.out.println(goods);
            if(travelService.addTravel(travel)){
                return new CommonResult(200,"插入成功！",null);
            }else {
                return new CommonResult(404,"插入失败！",null);
            }
        }

    }
    @PostMapping("/uploadCategory")
    public String uploadCategory(@RequestParam("name") String name){
        wxGoodsCategoryService.addCate(name);
        return "成功";
    }
}
